package cc.mrbird.unit.service.impl;

import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.service.impl.BaseService;
import cc.mrbird.system.domain.User;
import cc.mrbird.unit.dao.AuxiliaryInforMapper;
import cc.mrbird.unit.dao.ContactInforMapper;
import cc.mrbird.unit.dao.UnitInfoMapper;
import cc.mrbird.unit.dao.UserUnitMapper;
import cc.mrbird.unit.domain.*;
import cc.mrbird.unit.service.AuxiliaryInforService;
import cc.mrbird.unit.service.ContactInforService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service("auxiliaryInforService")
public class AuxiliaryInforServiceImpl extends BaseService<AuxiliaryInfor> implements AuxiliaryInforService {

    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private AuxiliaryInforMapper auxiliaryInforMapper;
    @Autowired
    private UserUnitMapper  userUnitMapper;
    @Autowired
    private UnitInfoMapper unitInfoMapper;
    @Override
    public List<AuxiliaryInfor> findAllList(AuxiliaryInfor auxiliaryInfor, QueryRequest request) {
        try {
            Example example = new Example(AuxiliaryInfor.class);
            Example.Criteria criteria = example.createCriteria();
            example.setOrderByClause("auxiliary_id");
            return this.selectByExample(example);
        } catch (Exception e) {
            log.error("error", e);
            return new ArrayList<>();
        }
    }

    @Override
    public void add(AuxiliaryInfor auxiliaryInfor,User user) {
        UserUnit unitIdByUser = userUnitMapper.findUnitIdByUser(user.getUserId());
        UnitInfo unitInfo = unitInfoMapper.findByUnitId(unitIdByUser.getUnit_id());
        auxiliaryInforMapper.save(auxiliaryInfor);
        Long auxiliary_id = auxiliaryInfor.getAuxiliary_id();
        unitInfoMapper.updateAuxiliaryInfor_id(auxiliary_id,unitInfo.getUnit_id());
    }

    @Override
    public void updateauxiliary(AuxiliaryInfor auxiliaryInfor) {
        this.auxiliaryInforMapper.updateauxiliary(auxiliaryInfor);
    }

    @Override
    public AuxiliaryInfor findById(Long id) {
        try {
            return this.auxiliaryInforMapper.findById(id);
        } catch (Exception e) {
            log.error("error", e);
            return new AuxiliaryInfor();
        }
    }

    @Override
    @Transactional
    public void delete(String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        this.batchDelete(list, "auxiliary_id", AuxiliaryInfor.class);
    }
}
